<template>
  <div>
    <div class="title">
      <el-menu
        :default-active="activeIndex"
        class="el-menu-demo"
        mode="horizontal"
        @select="stateBtn"
      >
        <el-menu-item index="1">审核中({{reviewCount.reviewingCount}})</el-menu-item>
        <el-menu-item index="9">审核驳回({{reviewCount.rejectCount}})</el-menu-item>
      </el-menu>
      <div>
        <el-button icon="el-icon-refresh" @click="refresh"></el-button>
      </div>
    </div>
    <div class="container">
      <div class="screeningBox">
        <div class="leftBox">
          <div class="leftitem">
            <el-form ref="conditions" :model="conditions">
              <el-select  v-if="userinfo.rolesId == 1 || userinfo.rolesId == 8" class="servicelevel" v-model="conditions.servicelevel" placeholder="服务商级别" @change="servicelevel">
                <el-option keys="1" label="一级服务商" value="1"></el-option>
                <el-option keys="2" label="二级服务商" value="2"></el-option>
              </el-select>

              <el-autocomplete v-if="userinfo.rolesId == 1 || userinfo.rolesId == 8 || userinfo.rolesId == 2" class="servicename" v-model.trim="conditions.servicename" :fetch-suggestions="querySearch" placeholder="服务商名称" @select="serviceid"></el-autocomplete>
              <el-autocomplete  class="BDname" v-model.trim="conditions.BDname" :fetch-suggestions="BDname" placeholder="业务经理" @select="BDid"></el-autocomplete>
            </el-form>
          </div>
          <div class="leftitem">
            <el-form ref="conditions" :model="conditions" label-width="80px">
              <el-input class="merchants" v-model.trim="conditions.merchantsno" placeholder="商户简称/商户名称/商户号"></el-input>
            </el-form>
          </div>
        </div>
        <div class="rightBox">
          <div class="rightitem">
            <div class="block">
              <el-date-picker
                v-model="conditions.date"
                type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="['00:00:00', '23:59:59']"
              ></el-date-picker>
            </div>
          </div>
          <div class="rightitem">
            <el-button icon="el-icon-search" type="primary" @click="soso">搜索</el-button>
            <el-button icon="el-icon-refresh" type="primary" @click="reset">重置</el-button>
          </div>
        </div>
      </div>
      <div class="tableBox">
        <el-table
          ref="multipleTable"
          :data="merchantarr"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
          stripe
          :header-cell-style="tableHeaderColor"
        >
          <el-table-column type="selection"></el-table-column>
          <el-table-column type="index" align="center" label="序号"></el-table-column>
          <el-table-column prop="createTime" align="center" label="提交时间"></el-table-column>
           <el-table-column
            label="商户号"
            align="center"
            width="160"
            :render-header="renderheader"
          >
            <template slot-scope="scope">
              <div>{{ scope.row.merchantNo }}
                 <i
                  class="el-icon-copy-document iconmove"
                  @click="copymerchantNoFunc(scope.row.merchantNo)"
                  v-clipboard:copy="copymerchantNo"
                  v-clipboard:success="onCopy"
                  v-clipboard:error="onError"
                /> 
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="merchantShortName" align="center" label="商户简称"></el-table-column>
          <el-table-column prop="merchantName" align="center" label="商户名称"></el-table-column>
          <el-table-column prop="agent2Name" align="center" label="服务商"></el-table-column>
         
          <el-table-column align="center" label="审核状态">
            <template slot-scope="scope">
              <div v-if='scope.row.status == 1'>审核中</div>
              <div v-else-if='scope.row.status == 9'>审核驳回</div>
              <div v-else-if='scope.row.status == 0' class='green'>正常</div>
            </template>
          </el-table-column>
          <el-table-column fixed="right" align="center" label="操作" width="120">
            <template slot-scope="scope">
                <el-button v-if="scope.row.status == 1 && (userinfo.rolesId == 1 || userinfo.rolesId == 8)" type="text" size="small" @click="joinmerchantsinfo(scope.row)">审核</el-button>
                <el-button v-if="oemFlag == 1 && oeminfo.checkMerchant == 1" type="text" size="small" @click="joinmerchantsinfo(scope.row)">审核</el-button>
                <el-button v-if="scope.row.status == 9 " type="text" size="small" @click="altermerchants(scope.row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination
            @current-change="handleCurrentChange"
            @size-change="sizechange"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          ></el-pagination>
        </div>
      </div>
      <br>
      <br>
    </div>
  </div>
</template>

<script>
import { merchantlist, getservice,getBD,getReviewCount } from "@/api/api";
export default {
  // name: "baseform",
  data: function() {
    return {
      copymerchantNo:'',
      activeIndex: '1',
      cur_page: 1,
      cur_size: 10,
      BDarr:[],
      multipleSelection: [],
      merchantarr: [],
      pageCount: "",
      total:10,
      reviewCount:"",
      userinfo:{},
      servicearr:[],
      conditions: {
        servicelevel: "",
        servicename: "",
        BDname: "",
        merchantsstate: 1,
        merchantsno: "",
        province:"",
        city:"",
        date:"",
        serviceid:"",
        BDid:"",
        wx1s:""
      },
      storesinfo: {
        stardate: "",
        enddate: "",
        storesno: "",
        merchantsno: "",
        state: ""
      },
      oeminfo:'',
      oemFlag:''
    };
  },
  created() {
    this.userinfo = JSON.parse(sessionStorage.getItem("userInfo"));
    this.merchantlist(this.userinfo.rolesId);
    this.getReviewCount()
    //  // 获取BD
    // getBD().then(res => {
    //       console.log(res);
    //       for(var i=0;i<res.data.rows.length;i++){
    //         this.BDarr.push({'value':res.data.rows[i].name,'id':res.data.rows[i].id})
    //       }
    //     });
     // 根据角色区别模糊搜索内容
    if (this.userinfo.rolesId == 2 || this.userinfo.rolesId == 3) {
      getservice({
        type: 2,
        parentid: this.userinfo.fid
      }).then(res => {
        // console.log(res);
        for (var i = 0; i < res.data.rows.length; i++) {
          this.servicearr.push({
            value: res.data.rows[i].agentname,
            id: res.data.rows[i].id
          });
        }
      });
      this.getBD(this.userinfo.fid);
    } else {
      this.getBD();
    }
    // 判断是否是oem
    this.oeminfo = JSON.parse(sessionStorage.getItem('oeminfo'))
    this.oemFlag = sessionStorage.getItem('oemFlag')

  },
  mounted() {},
  methods: {
           //复制商户号
    copymerchantNoFunc(item){
      this.copymerchantNo = item.replace(/^\s+|\s+$/g, "");
    },
        onCopy: function (e) {
      this.$message({
        message: "复制成功",
        type: "success",
      });
      console.log("复制成功");
    },
    onError: function (e) {
      this.$message.error("复制失败");
      console.log("复制失败");
    },
    //修改thead 背景色
     tableHeaderColor({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return 'background-color: #F0F0F0;font-weight: 500;'
      }
    },
   // 获取BD
    getBD(id) {
      this.BDarr = [];
      getBD({ agentid: id }).then(res => {
        // console.log(res);
        for (var i = 0; i < res.data.rows.length; i++) {
          this.BDarr.push({
            value: res.data.rows[i].name,
            id: res.data.rows[i].id
          });
        }
      });
    },
    // 分页导航
    handleCurrentChange(val) {
      this.cur_page = val;
      this.merchantlist();
      this.$nextTick(() => {
        document.querySelectorAll(".content")[0].scrollTop = "0";
      });
    },
    // 分页数量
    sizechange(val) {
      this.cur_size = val;
      this.merchantlist();

      this.$nextTick(() => {
        document.querySelectorAll(".content")[0].scrollTop = "0";
      });
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    joinmerchantsinfo(val) {
      this.$router.push({ path: "/auditdetails"  });
      sessionStorage.setItem('auditinfo',JSON.stringify(val))
    },
     // 商户列表
    merchantlist(level) {
      let sosolist = {};
      if (level == 1) {
        sosolist = {
          page: this.cur_page,
          size: this.cur_size,
          agent1Id: this.conditions.serviceid,
          bdId: this.conditions.BDid,
          startTime: this.conditions.date[0],
          finishTime: this.conditions.date[1],
          cityCode: this.conditions.city,
          merchantMcc2: this.conditions.wx1s,
          provinceCode: this.conditions.provincecode,
          keywords: this.conditions.merchantsno,
          status: this.conditions.merchantsstate
        };
      } else if (level == 2) {
        sosolist = {
          page: this.cur_page,
          size: this.cur_size,
          agent2Id: this.conditions.serviceid,
          bdId: this.conditions.BDid,
          startTime: this.conditions.date[0],
          finishTime: this.conditions.date[1],
          cityCode: this.conditions.city,
          merchantMcc2: this.conditions.wx1s,
          provinceCode: this.conditions.provincecode,
          keywords: this.conditions.merchantsno,
          status: this.conditions.merchantsstate
        };
      }else{
         sosolist = {
          page: this.cur_page,
          size: this.cur_size,
          bdId: this.conditions.BDid,
          startTime: this.conditions.date[0],
          finishTime: this.conditions.date[1],
          cityCode: this.conditions.city,
          merchantMcc2: this.conditions.wx1s,
          provinceCode: this.conditions.provincecode,
          keywords: this.conditions.merchantsno,
          status: this.conditions.merchantsstate
        };
      }
      merchantlist(sosolist).then(res => {
        // console.log(res);
        if (res.code == 405) {
          sessionStorage.removeItem("token");
          this.$router.push("/login");
        } else {
          this.merchantarr = res.data;
          this.combined = res.pageInfo.recordCount;
          this.pageCount = res.pageInfo.pageCount;
          this.loading = false;
          this.total = res.pageInfo.recordCount
        }
      });
    },
    // 搜索
    soso() {
      if(this.conditions.date == null || this.conditions.date.length == 0 || this.conditions.date == undefined) {
        this.conditions.date = []
      }
      if(this.conditions.servicelevel){
        this.merchantlist(this.conditions.servicelevel);
      }else{
        this.merchantlist(this.userinfo.rolesId);
      }
      
    },
    // 重置
    reset() {
      this.conditions.date = "";
      this.conditions.merchantsno = "";
      this.conditions.servicelevel = "";
      this.conditions.servicename = "";
      this.conditions.BDname = "";
      this.conditions.BDid = "";
      this.conditions.serviceid = "";
    },
    // 刷新
    refresh() {
      // location.reload()
      this.$router.go({
        path: 0,
        query: {
          t: Date.now()
        }
      })
    },
     // 业务经理
    BDname(queryString, cb){
      let BDarr = this.BDarr;
      let results = queryString
        ? BDarr.filter(this.createFilter(queryString))
        : BDarr;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    // 服务商级别
    servicelevel(val){
      this.servicearr = []
       getservice({
          type: val,
        }).then(res => {
          console.log(res);
          for(var i=0;i<res.data.rows.length;i++){
            this.servicearr.push({'value':res.data.rows[i].agentname,'id':res.data.rows[i].id})
          }
        });

    },
     // 服务商名称
     querySearch(queryString, cb) {
      let servicearr = this.servicearr;
      let results = queryString
        ? servicearr.filter(this.createFilter(queryString))
        : servicearr;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
     createFilter(queryString) {
      return restaurant => {
        return (
          restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) !==
          -1
        );
      };
    },
     serviceid(item){
      this.conditions.serviceid = item.id
      this.getBD(item.id);
    },
      BDid(item) {
      this.conditions.BDid = item.id
    },
    // 状态btn
    stateBtn(val){
      console.log(val)
      this.conditions.merchantsstate = val
      this.cur_page = 1;
      this.reset()
      this.merchantlist(this.userinfo.rolesId)
    },
    // 获取商户进件审核数量
    getReviewCount(){
       getReviewCount({}).then(res => {
                if (res.code == 405) {
                    sessionStorage.removeItem("token");
                    this.$router.push("/login");
                    } else if (res.code == 200) {
                      this.reviewCount = res.data
                    }else{
                        this.$message.error(res.message);
                    }
                
            });
    },
    // 修改商户
    altermerchants(val){
      this.$router.push({ path: "/altermerchants"});
      sessionStorage.setItem('auditinfo',JSON.stringify(val))
      sessionStorage.removeItem("channel");
    },
    // 跳详情
     addmerdetails(val) {
      this.$router.push({ path: "/addmerdetails"  });
      sessionStorage.setItem('auditinfo',JSON.stringify(val))
    },
  }
};
</script>
<style scoped lang="less">
.iconmove{
  cursor:pointer;
}
.title {
  background: #fff;
  padding: 10px 10px;
  font-size: 18px;
  font-weight: 500;
  display: flex;
  justify-content: space-between;
}
.name {
  width: 25%;
}
.screeningBox {
  width: 100%;
  display: flex;
}
.leftBox,
.rightBox {
  width: 50%;
  padding: 0 15px;
  box-sizing: border-box;
}
.leftBox > .leftitem {
  margin-bottom: 15px;
}
.rightBox > .rightitem {
  margin-bottom: 15px;
}
.merchants {
  width: 50%;
}
.statistical {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #00152a;
  color: #fff;
  padding: 12px;
  box-sizing: border-box;
}
.statistical > .statisticalitem {
  width: 50%;
  text-align: center;
  line-height: 200%;
}
.tableBox {
  margin-top: 20px;
}

.red {
  color: #f9607d;
}

.my-autocomplete {
  li {
    line-height: normal;
    padding: 7px;

    .name {
      text-overflow: ellipsis;
      overflow: hidden;
    }
    .addr {
      font-size: 12px;
      color: #b4b4b4;
    }

    .highlighted .addr {
      color: #ddd;
    }
  }
}
.green{
  color:green;
}
</style>